/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "WZUOLProperty.h"

#include "WZLibUtils.h"
#include "WZSubProperty.h"
#include "WZCanvasProperty.h"
#include "WZConvexProperty.h"
#include "WZExtendedProperty.h"

using namespace std;

namespace WZLib{
	WZImageProperty* WZUOLProperty::prop(){return this->_prop;}
	ctor WZUOLProperty::WZUOLProperty(WZImage* parent,string name):WZStringProperty("",parent,name){
		this->_prop=NULL;
		this->_propType=Prop_UOL;
	}
	ErrorCode WZUOLProperty::parse(WZLib::WZBinaryReader* reader){
		return Error_WZLib_WZUOLProperty_Parse_WrongParseFunction;
	}
	ErrorCode WZUOLProperty::parse(WZLib::WZBinaryReader *reader,int offset){
		reader->IncPosition(1);
		byte b=reader->ReadByte();
		string val="";
		switch(b){
			case 0:
				val=reader->ReadEncryptedString();
				break;
			case 1:
				val=reader->ReadStringAtOffset(offset+reader->ReadInt32());
				break;
		}
		this->value=val;
		this->_parsed=true;
		return Error_NoError;
	}
	WZImageProperty* WZUOLProperty::resolve(){
		vector<string> instr=tokenize(this->value,"/");
		WZImageProperty* curProp=(WZImageProperty*)(*(this->parent()));
		//curProp=(WZImageProperty*)(*(curProp->parent()));
		for(unsigned int i=0,l=instr.size();i<l;i++){
			if(instr[i]==".."){//go up one
				curProp=(WZImageProperty*)(*(curProp->parent()));
				while(curProp->propType()==Prop_Extended)
					curProp=(WZImageProperty*)(*(curProp->parent()));
			}else{//go down one
				if(((int)curProp->propType())/100==WZImageProperty::Prop_Sub)
					curProp=(WZImageProperty*)(((WZSubProperty*)curProp)->properties()[instr[i]]);
				/*switch(curProp->propType()){
					case Prop_Sub:
						curProp=(WZImageProperty*)(((WZSubProperty*)curProp)->properties()[instr[i]]);
						break;
					case Prop_Canvas:
						curProp=(WZImageProperty*)(((WZCanvasProperty*)curProp)->properties()[instr[i]]);
						break;
					case Prop_Convex:
						curProp=(WZImageProperty*)(((WZConvexProperty*)curProp)->properties()[instr[i]]);
						break;
					default:
						break;
				}//*/
			}
		}
		WZImageProperty* downProp=NULL;
		switch(curProp->propType()){
			case Prop_Sub:
				downProp=(WZImageProperty*)((WZSubProperty*)curProp)->properties()[this->name()];
				break;
			case Prop_Canvas:
				downProp=(WZImageProperty*)((WZCanvasProperty*)curProp)->properties()[this->name()];
				break;
			case Prop_Convex:
				downProp=(WZImageProperty*)((WZConvexProperty*)curProp)->properties()[this->name()];
				break;
		}
		if(downProp!=NULL)
			this->_prop=downProp;
		else
			this->_prop=curProp;
		return curProp;
	}
};